package com.g2room.myuall.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HtmlUtil {

	private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>"; // 定义script的正则表达式  
    private static final String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>"; // 定义style的正则表达式  
    private static final String regEx_iframe = "<iframe[^>]*?>[\\s\\S]*?<\\/iframe>"; // 定义style的正则表达式  
//  private static final String regEx_note = "<!--.[^-]*(?=-->)-->"; // 定义注释的正则表达式 
  private static final String regEx_note = "<!--[^>]*?[\\s\\S]*?-->"; // 定义注释的正则表达式 
    private static final String regEx_div = "<div  class=\"rv-root-v2 rv-js-root\"[^>]*?>[\\s\\S]*?<\\/div>"; // 定义注释的正则表达式  
    private static final String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式  
    private static final String regEx_space = "\\s*|\t|\r|\n";//定义空格回车换行符 
    

    private static final String regEx_span = "<span class=\"news_related\">[\\s\\S]*?</span>"; // 定义style的正则表达式  
    //<span class="news_related">

    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delHTMLTag(String htmlStr) {  
//        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
//        Matcher m_script = p_script.matcher(htmlStr);  
//        htmlStr = m_script.replaceAll(""); // 过滤script标签  
//  
//        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
//        Matcher m_style = p_style.matcher(htmlStr);  
//        htmlStr = m_style.replaceAll(""); // 过滤style标签  
    	htmlStr = delScriptAndStyleTag(htmlStr.trim());
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
        Matcher m_html = p_html.matcher(htmlStr);  
        htmlStr = m_html.replaceAll(""); // 过滤html标签  
  
        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
        Matcher m_space = p_space.matcher(htmlStr);  
        htmlStr = m_space.replaceAll(""); // 过滤空格回车标签  
        return htmlStr; // 返回文本字符串  
    }
    
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delScriptAndStyleTag(String htmlStr) {  
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
        Matcher m_script = p_script.matcher(htmlStr);  
        htmlStr = m_script.replaceAll(""); // 过滤script标签  
  
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
        Matcher m_style = p_style.matcher(htmlStr);  
        htmlStr = m_style.replaceAll(""); // 过滤style标签  
        
        Pattern p_iframe = Pattern.compile(regEx_iframe, Pattern.CASE_INSENSITIVE);  
        Matcher m_iframe = p_iframe.matcher(htmlStr);  
        htmlStr = m_iframe.replaceAll(""); // 过滤iframe标签  
        
//        Pattern p_note = Pattern.compile(regEx_note, Pattern.CASE_INSENSITIVE);  
//        Matcher m_note = p_note.matcher(htmlStr);  
//        htmlStr = m_note.replaceAll(""); // 过滤note标签  
//        htmlStr = delNoteTag(htmlStr);
        return htmlStr.trim(); // 返回文本字符串  
    }
    
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delNoteTag(String htmlStr) {
        Pattern p_note = Pattern.compile(regEx_note, Pattern.CASE_INSENSITIVE);  
        Matcher m_note = p_note.matcher(htmlStr);  
        htmlStr = m_note.replaceAll(""); // 过滤note标签  
        return htmlStr.trim(); // 返回文本字符串  
    }
    
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delSpanTag(String htmlStr) {
        Pattern p_note = Pattern.compile(regEx_span, Pattern.CASE_INSENSITIVE);  
        Matcher m_note = p_note.matcher(htmlStr);  
        htmlStr = m_note.replaceAll(""); // 过滤note标签  
        return htmlStr.trim(); // 返回文本字符串  
    }
    
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delDivTag(String htmlStr) {
    	Pattern p_div = Pattern.compile(regEx_div, Pattern.CASE_INSENSITIVE);  
        Matcher m_div = p_div.matcher(htmlStr);  
        htmlStr = m_div.replaceAll(""); // 过滤div标签
        return htmlStr.trim(); // 返回文本字符串  
    }
    
    /**
     * 当前目录下的当前日期的目录
     * @param basePath
     * @return
     */
    public static String getPath(String basePath) {
        return basePath + getDateDir() + "/";
    }
    
    public static String getDateDir() {
    	Calendar cal = Calendar.getInstance();
        int month = cal.get(Calendar.MONTH) + 1;
        int day = cal.get(Calendar.DAY_OF_MONTH);
    	return (cal.get(Calendar.YEAR) + "" + (month > 9 ? month : "0" + month) + "" + (day > 9 ? day : "0" + day));
    }
    
    /** 
     * @param htmlStr 
     * @return 
     *  删除Html标签 
     */  
    public static String delTag(String tag, String htmlStr) {
        Pattern p_note = Pattern.compile("<"+tag+"[^>]*?>[\\s\\S]*?<\\/"+tag+">", Pattern.CASE_INSENSITIVE);  
        Matcher m_note = p_note.matcher(htmlStr);  
        htmlStr = m_note.replaceAll(""); // 过滤tag标签  
        return htmlStr.trim(); // 返回文本字符串  
    }
    /**
     * 删除A标签，保留里面的文字
     * @param html
     * @return
     */
    public static String delATag(String html) {
		html=html.replaceAll("<a [^>]*>", "").replaceAll("</a>", "&emps;");
		return html;
	}
    /**
     * 删除注释、A和js style标签，保留里面的文字
     * @param html
     * @return
     */
    public static String delAllTag(String html) {
    	html = HtmlUtil.delNoteTag(html);
    	html = HtmlUtil.delATag(html);
    	html = HtmlUtil.delScriptAndStyleTag(html);
		return html;
	}
    
    public static void main(String[] args) {
    	String h = "<a class=\"ShuKeyWordLink\" href=\"/shuyu/detail_3_6_408.html?lang=408 \" target=\"_blank\">助</a><a href=\"aa\">里</a><aa href=\"aa\">里</aa>";
    	System.out.println(delATag(h));
    }
    
    public static Date string2Date(String dateStr, String pattern) throws ParseException {
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    	Date date = sdf.parse(dateStr);
    	return date;
    }
    
    public static String date2String(Date date, String pattern) {
    	SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    	String dateStr = null;
    	try {
    		dateStr = sdf.format(date);
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return dateStr;
    }

    /**
     * 获取当前时间
     * @param h	偏移的小时数
     * @return
     */
	public static String getNow() {
		return getNow(3);
	}

    /**
     * 获取当前时间
     * @param h	偏移的小时数
     * @return
     */
	public static String getNow(int h) {
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + h);
		return date2String(cal.getTime(), "yyyy-MM-dd hh:mm:ss");
	}
	
	public static String handleSpace(String str) {
		return str.replaceAll("&nbsp;", " ");
	}
	

	public static boolean isEmpty(String str) {
		return null == str || str.length() == 0;
	}
}
